package fr.ssd.ci.ui.panel;

import java.awt.BorderLayout;
import java.awt.Insets;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import fr.ssd.ci.certificat.Installer;
import fr.ssd.ci.ui.action.InstallAction;
import fr.ssd.ci.ui.action.KeyStoreChooserAction;

public class MainPanel extends JPanel {

	private static final long serialVersionUID = 886976800956285691L;

	static private final String newline = "\n";

	Installer ci;

	JButton installButton;

	JLabel hostLabel;
	JTextField hostField;

	JLabel keyStoreLabel;
	JTextField keyStoreField;
	JButton keyStoreButton;

	JLabel passPhraseLabel;
	JTextField passPhraseField;

	JTextArea log;

	public MainPanel() {
		super(new BorderLayout());

		ci = new Installer();

		hostLabel = new JLabel("Host[:port]", JLabel.TRAILING);
		hostField = new JTextField(40);
		hostLabel.setLabelFor(hostField);

		keyStoreLabel = new JLabel("Keystore", JLabel.TRAILING);
		keyStoreField = new JTextField(40);
		keyStoreField.setText(ci.getDefaultKeyStoreFileName());
		keyStoreLabel.setLabelFor(keyStoreField);

		passPhraseLabel = new JLabel("PassPhrase", JLabel.TRAILING);
		passPhraseField = new JPasswordField("changeit", 40);
		passPhraseLabel.setLabelFor(passPhraseField);

		// Create the log first, because the action listeners
		// need to refer to it.
		log = new JTextArea(5, 40);
		log.setMargin(new Insets(5, 5, 5, 5));
		log.setEditable(false);
		JScrollPane logScrollPane = new JScrollPane(log);

		keyStoreButton = new JButton("Chose keystore");
		keyStoreButton.addActionListener(new KeyStoreChooserAction(this,
				keyStoreField));

		installButton = new JButton("Install certificat");
		installButton.addActionListener(new InstallAction(hostField,
				keyStoreField, passPhraseField, ci));

		final JPanel formPanel = new JPanel();
		GroupLayout layout = new GroupLayout(formPanel);
		formPanel.setLayout(layout);

		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		layout.setHorizontalGroup(layout
				.createSequentialGroup()
				.addGroup(
						layout.createParallelGroup(
								GroupLayout.Alignment.LEADING)
								.addComponent(hostLabel)
								.addComponent(keyStoreLabel)
								.addComponent(passPhraseLabel))
				.addGroup(
						layout.createParallelGroup(
								GroupLayout.Alignment.LEADING)
								.addComponent(hostField)
								.addComponent(keyStoreField)
								.addComponent(passPhraseField))
				.addGroup(
						layout.createParallelGroup(
								GroupLayout.Alignment.LEADING)
								.addComponent(keyStoreButton)
								.addComponent(installButton)));
		layout.setVerticalGroup(layout
				.createSequentialGroup()
				.addGroup(
						layout.createParallelGroup(
								GroupLayout.Alignment.BASELINE)
								.addComponent(hostLabel)
								.addComponent(hostField))
				.addGroup(
						layout.createParallelGroup(
								GroupLayout.Alignment.BASELINE)
								.addComponent(passPhraseLabel)
								.addComponent(passPhraseField))
				.addGroup(
						layout.createParallelGroup(
								GroupLayout.Alignment.BASELINE)
								.addComponent(keyStoreLabel)
								.addComponent(keyStoreField)
								.addComponent(keyStoreButton))
				.addComponent(installButton));

		add(formPanel, BorderLayout.NORTH);
		add(new JSeparator(), BorderLayout.CENTER);
		add(logScrollPane, BorderLayout.SOUTH);
	}

}